/*
* 文件名：SignatureDetail/index.js
* 创建人：邱建
* 创建时间：2019-12-17
* 描述： 签名板调用界面
*/
import React, { Component } from 'react';
import { connect } from 'dva';
import { SignaturePad } from "components/signature-pad"
import router from 'umi/router';
import { Toast } from 'antd-mobile'
import { returnUrl as url } from 'utils/ip'
import { dingTalkBack, isAndroidOrIos, closeTheCurrentPage } from 'utils/utils';

@connect(({ sealAndSignatureModel, pdfModel }) => ({ sealAndSignatureModel, pdfModel }))
class SignatureDetail extends Component {
	constructor(props) {
		super(props)
		this.state = {
			signaturePadIsOpen: true,
		}
	}
	componentDidMount() {
		
	}

	// saveReturnUrl=()=>{
	//   if(localStorage.getItem("isDingTalk")==='true'){
	//     const phoneType=isAndroidOrIos()
	//     const backUrl=url()+'/Signature'
	//     if(phoneType==='iPhone'){
	//
	//       document.addEventListener('backbutton', function(e) {
	//         e.preventDefault(); //backbutton事件的默认行为是回退历史记录，如果你想阻止默认的回退行为，那么可以通过preventDefault()实现
	//         // dingTalkBack(backUrl)
	//       },false);
	//     }else {
	//       console.log(5346)
	//       document.addEventListener('backbutton', function(e) {
	//         // 在这里处理你的业务逻辑
	//         console.log(backUrl)
	//         e.preventDefault(); //backbutton事件的默认行为是回退历史记录，如果你想阻止默认的回退行为，那么可以通过preventDefault()实现
	//         dingTalkBack(backUrl)
	//       },false);
	//     }
	//   }
	// }
	//确认签名
	onSignaturePadConfirm = async data => {
		const { dispatch, } = this.props;
		const usid = localStorage.getItem('userWsid');
		Toast.loading('生成图片中', 0)
		await dispatch({
			type: 'sealAndSignatureModel/postSignatureAdd',
			payload: {
				name: '手写签名',
				from: 'TEXT_INPUT',
				authorWsid: usid,
				base64: data.imageData.replace(/data:image\/.*;base64,/, '')
			},
		})
		await dispatch({
			type: 'sealAndSignatureModel/getSignatureList',
			payload: {
				author_wsid: usid,
			},
			callback: () => {
				Toast.hide()
				//此处调用接口上传图片
				this.closeSignaturePad()
			}
		})

	}

	//关闭签名版
	closeSignaturePad() {
		const { location: { query: { result } } } = this.props;
		this.setState({
			signaturePadIsOpen: false,
		})
		if (result) {
			router.push({
				pathname: '/Signature',
				query: {
					result: result
				},
				params: {
					type: 'WRITE_SIGN',
				}
			})
		} else {
			router.push({
				pathname: '/Signature',
				// query:{
				//   result:'success'
				// },
				params: {
					type: 'WRITE_SIGN',
				}
			})

		}

	}

	render() {
		return (
			<div>
				<SignaturePad isOpen={this.state.signaturePadIsOpen} onConfirm={this.onSignaturePadConfirm} onCancel={() => this.closeSignaturePad()} />
			</div>
		)
	}
}

export default SignatureDetail;
